package org.termine.logic;

import java.sql.Connection;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.termine.dao.LocationDAO;
import org.termine.dao.PersonalDataDAO;
import org.termine.entity.Location;
import org.termine.entity.PersonalData;
import org.termine.util.GeoCoder;

@ManagedBean
@ApplicationScoped
public class PersonalDataManager extends BaseManager {

	private static final Logger logger = LoggerFactory.getLogger(AvailabilityManager.class);

	@ManagedProperty(value = "#{locationDAO}")
	private LocationDAO locationDAO;
	@ManagedProperty(value = "#{personalDataDAO}")
	private PersonalDataDAO personalDataDAO;

	public boolean addProvider(PersonalData personalData, Location location) {
		Connection connection = null;
		try {
			connection = this.connector.getConnection();

			GeoCoder.setPosition(location);
			this.getLocationDAO().addLocation(connection, location);
			location.setLocation_id(this.getLocationDAO().getLocationId(connection, location));
			personalData.setLocation_id(location.getLocation_id());

			this.getPersonalDataDAO().addPersonalData(connection, personalData);
			personalData.setPersonal_data_id(this.getPersonalDataDAO().getPersonalDataId(connection, personalData));

			this.getPersonalDataDAO().addClient(connection, personalData.getPersonal_data_id());
			personalData.setClient_id(this.getPersonalDataDAO().getClientId(connection, personalData.getPersonal_data_id()));
			this.getPersonalDataDAO().addProvider(connection, personalData.getPersonal_data_id());
			personalData.setProvider_id(this.getPersonalDataDAO().getProviderId(connection, personalData.getPersonal_data_id()));

			this.getPersonalDataDAO().updatePersonalData(connection, personalData);

			this.connector.commit(connection);
			return true;
		} catch (Exception e) {
			this.connector.rollback(connection);
			logger.error("ERROR: ", e);
			return false;
		}
	}

	public boolean addClient(PersonalData personalData, Location location) {
		Connection connection = null;
		try {
			connection = this.connector.getConnection();

			GeoCoder.setPosition(location);

			this.getLocationDAO().addLocation(connection, location);
			location.setLocation_id(this.getLocationDAO().getLocationId(connection, location));
			personalData.setLocation_id(location.getLocation_id());

			this.getPersonalDataDAO().addPersonalData(connection, personalData);
			personalData.setPersonal_data_id(this.getPersonalDataDAO().getPersonalDataId(connection, personalData));

			this.getPersonalDataDAO().addClient(connection, personalData.getPersonal_data_id());
			personalData.setClient_id(this.getPersonalDataDAO().getClientId(connection, personalData.getPersonal_data_id()));

			this.getPersonalDataDAO().updatePersonalData(connection, personalData);

			this.connector.commit(connection);
			return true;
		} catch (Exception e) {
			logger.error("ERROR: ", e);
			return false;
		}
	}

	public PersonalData getPersonalData(String email, String password) {
		Connection connection = null;
		try {
			connection = this.connector.getConnection();
			PersonalData personalData = this.getPersonalDataDAO().getPersonalData(connection, email, password);
			return personalData;
		} catch (Exception e) {
			logger.error("ERROR: ", e);
			return null;
		} finally {
			this.connector.closeConnection(connection);
		}
	}

	public boolean updatePersonalData(PersonalData personalData) {
		Connection connection = null;
		try {
			connection = this.connector.getConnection();
			this.getPersonalDataDAO().updatePersonalData(connection, personalData);
			this.connector.commit(connection);
			return true;
		} catch (Exception e) {
			this.connector.rollback(connection);
			logger.error("ERROR: ", e);
			return false;
		}
	}

	/*
	 * Getters & Setters
	 */
	public LocationDAO getLocationDAO() {
		return this.locationDAO;
	}

	public void setLocationDAO(LocationDAO locationDAO) {
		this.locationDAO = locationDAO;
	}

	public PersonalDataDAO getPersonalDataDAO() {
		return this.personalDataDAO;
	}

	public void setPersonalDataDAO(PersonalDataDAO personalDataDAO) {
		this.personalDataDAO = personalDataDAO;
	}
}
